package com.jn.algorithm.day020.question60;

import java.util.*;

/**
 * @author 江南大学1033190417
 * @date 2022/5/11 15:12
 */
public class Solution {

    public static int index=0;
    public static String res="";

    public String getPermutation(int n, int k) {
        StringBuilder path = new StringBuilder();
        boolean[] used = new boolean[n + 1];
        dfs(path, used, 0, n, k);
        return res;
    }

    private void dfs(StringBuilder path, boolean[] used, int depth, int n, int k) {
        if (depth == n) {
            ++index;
            if (index==k){
                res=path.toString();
            }
            return;
        }
        for (int i = 1; i <= n && index < k; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            path.append(i);
            dfs(path, used, depth + 1, n, k);
            used[i] = false;
            path.deleteCharAt(path.length() - 1);
        }
    }


    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.getPermutation(4, 9));

    }
}
